package com.controller;

import com.entity.Result;
import com.service.MessageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
public class MessageController {

    @Autowired
    MessageServer messageServer;

    //查询消息
    @RequestMapping("getInfos")
    public Result getInfos(String date, HttpServletRequest request){
        Result result=messageServer.getInfos(date,request);
        return result;
    }

    //确认消息
    @RequestMapping("confirmInfo")
    public Result confirmInfo(Integer mid){
        try{
            messageServer.confirmInfo(mid);
            return new Result(true,"确认成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"确认失败");
        }
    }

    //得到消息个数
    @RequestMapping("getInfoNum")
    public Result getInfoNum(HttpServletRequest request){
        Result result=messageServer.getInfoNum(request);

        return result;
    }
    //发送消息给管理员
    @RequestMapping("sendToAdmin")
    public Result sendText(String title,String sendText,HttpServletRequest request){
        try{
            messageServer.sendText(title,sendText,request);
            return new Result(true,"发送成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"发送失败");
        }
    }
}
